<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="orderDialogFormRef" :rules="rules" :model="ruleForm" size="default" label-width="100px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="公司名称" prop="name">
							<el-input v-model="ruleForm.name" placeholder="请输入公司名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="公司电话" prop="tel">
							<el-input v-model="ruleForm.tel" placeholder="请输入公司电话" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="联系人" prop="contactName">
							<el-input v-model="ruleForm.contactName" placeholder="请输入联系人姓名" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="手机号" prop="contactTel">
							<el-input v-model="ruleForm.contactTel" placeholder="请输入联系人手机号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户登录名" prop="account">
							<el-input v-model="ruleForm.account" placeholder="请输入账户名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="账户密码" prop="password">
							<el-input v-model="ruleForm.password" placeholder="请输入6-20位密码" type="password" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="公司地址" prop="address">
							<el-input v-model="ruleForm.address" type="textarea" placeholder="请输入公司地址" maxlength="150"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref, nextTick } from 'vue';
import { useOrder, OrderVo } from './order';
import { ElMessage } from 'element-plus';
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const orderDialogFormRef = ref();
const ruleForm = reactive<OrderVo>({
	name: '', // 公司名称
	tel: '', // 公司电话
	contactName: '', // 联系人
	contactTel: '', // 联系电话
	address: '', // 地址
	account: '', // 账号
	password: '', // 密码
});
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const rules = ref({
	name: [
		{ required: true, type: 'string', message: '公司名称不能为空', trigger: 'blur' },
		{ pattern: /[\u4e00-\u9fa5A-Za-z0-9]/, min: 2, max: 20, message: '请输入2-20个中英文字符名称', trigger: 'blur' },
	],
	tel: [
		{ required: true, type: 'string', message: '电话不能为空', trigger: 'blur' },
		{ pattern: /[0-9\-（）()]+/, type: 'string', message: '请输入正确的号码格式', trigger: 'blur' },
	],
	contactName: [
		{ required: true, type: 'string', message: '联系人不能为空', trigger: 'blur' },
		{ pattern: /[\u4e00-\u9fa5A-Za-z0-9]/, min: 2, max: 20, message: '请输入2-20个中英文字符名称', trigger: 'blur' },
	],
	contactTel: [
		{ required: true, type: 'string', message: '联系电话不能为空', trigger: 'blur' },
		{ pattern: /[0-9]{11}/, len: 11, message: '请输入正确的手机号码', trigger: 'blur' },
	],
	address: [{ required: false, type: 'string', max: 100, message: '地址不能超过100个字符', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (type: string, row: any) => {
	if (type === 'edit') {
		console.log(row);
		// ruleForm.value = row;
		state.dialog.title = '修改信息';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增厂商';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		nextTick(() => {
			orderDialogFormRef.value.resetFields();
		});
	}
	state.dialog.isShowDialog = true;
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (!orderDialogFormRef.value) return;
	orderDialogFormRef.value.validate((valid:any) => {
		if (valid) {
			useOrder()
				.saveOrder(ruleForm)
				.then(() => {
					closeDialog();
					emit('refresh');
				})
				.then(() => {
					ElMessage.success('创建成功！');
				});
		}
	});
	// closeDialog();
	// emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
./order